package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.pojo.PageResult;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.TbSellerExample;
import com.dongdongshop.service.TbSellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@DubboService
public class TbSellerServiceImpl implements TbSellerService {

    //注入mappaer
    @Autowired
    private TbSellerMapper tbSellerMapper;

    //注册(添加)
    @Override
    public void addTbSeller(TbSeller tbSeller) {
        tbSellerMapper.insertSelective(tbSeller);
    }

    //根据sellerId查询数据是否存在
    @Override
    public List<TbSeller> showbysellerId(String sellerId) {
        TbSellerExample te = new TbSellerExample();
        TbSellerExample.Criteria criteria = te.createCriteria();
        criteria.andSellerIdEqualTo(sellerId);
        return tbSellerMapper.selectByExample(te);
    }

    //查询商家全部数据(要求分页)
    @Override
    public PageResult<TbSeller> TbSellershowall(Integer pageNum, Integer pageSize,TbSeller tbSeller) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSeller> list = tbSellerMapper.TbSellershowall(tbSeller);
        PageInfo info = new PageInfo(list);
        return new PageResult<>(pageNum,pageSize,info.getTotal(),info.getList(),info.getPages());
    }

    //查询单个商家全部数据
    @Override
    public TbSeller TbSellershowbysellerId(String sellerId) {
        return tbSellerMapper.selectByPrimaryKey(sellerId);
    }

    //修改单个商家数据
    @Transactional
    @Override
    public void updateTbSeller(TbSeller tbSeller) {
        TbSeller tbSeller1 = tbSellerMapper.selectByPrimaryKey(tbSeller.getSellerId());
        tbSeller1.setStatus(tbSeller.getStatus());
        tbSellerMapper.updateByPrimaryKeySelective(tbSeller1);
    }

    //查看账号是否存在
    @Override
    public TbSeller findTbAdminByName(String sellerId) {
        TbSellerExample te = new TbSellerExample();
        TbSellerExample.Criteria criteria = te.createCriteria();
        criteria.andSellerIdEqualTo(sellerId);
        List<TbSeller> list = tbSellerMapper.selectByExample(te);
        return (list.size()==0?null:list.get(0));
    }
}
